import 'package:efood_multivendor/data/model/response/product_model.dart';

class CheckoutApiResponse {
  int? code;
  String? message;
  CheckoutData? data;

  CheckoutApiResponse({this.code, this.message, this.data});

  CheckoutApiResponse.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    message = json['message'];
    data =
        json['data'] != null ? new CheckoutData.fromJson(json['data']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['code'] = this.code;
    data['message'] = this.message;
    if (this.data != null) {
      data['data'] = this.data?.toJson();
    }
    return data;
  }
}

class CheckoutData {
  List<ProductWithQuantity>? items;
  double? subTotal;
  double? serviceCharge;
  double? deliveryCharge;
  double? languageFee;
  double? orderAmount;
  Coupon? coupon;
  double? couponDiscountItem;
  double? couponDiscountDelivery;
  double? couponDiscountAmount;
  double? addonAmount;
  double? discountAmount;

  CheckoutData(
      {this.items,
      this.subTotal,
      this.serviceCharge,
      this.deliveryCharge,
      this.languageFee,
      this.orderAmount,
      this.coupon,
      this.couponDiscountItem,
      this.couponDiscountDelivery,
      this.couponDiscountAmount});

  CheckoutData.fromJson(Map<String, dynamic> json) {
    if (json['items'] != null) {
      items = [];
      json['items'].forEach((v) {
        items?.add(new ProductWithQuantity.fromJson(v));
      });
    }
    subTotal = json['sub_total'];
    serviceCharge = json['service_charge'];
    deliveryCharge = json['delivery_charge'];
    languageFee = json['language_fee'];
    orderAmount = json['order_amount'];
    coupon =
        json['coupon'] != null ? new Coupon.fromJson(json['coupon']) : null;
    couponDiscountItem = json['coupon_discount_item'];
    couponDiscountDelivery = json['coupon_discount_delivery'];
    couponDiscountAmount = json['coupon_discount_amount'];
    addonAmount = json['add_on_amount'];
    discountAmount =
        json['discount_amount'] != null ? json['discount_amount'] : 0;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.items != null) {
      data['items'] = this.items?.map((v) => v.toJson()).toList();
    }
    data['sub_total'] = this.subTotal;
    data['service_charge'] = this.serviceCharge;
    data['delivery_charge'] = this.deliveryCharge;
    data['language_fee'] = this.languageFee;
    data['order_amount'] = this.orderAmount;
    data['coupon'] = this.coupon;
    data['coupon_discount_item'] = this.couponDiscountItem;
    data['coupon_discount_delivery'] = this.couponDiscountDelivery;
    data['coupon_discount_amount'] = this.couponDiscountAmount;
    return data;
  }
}

class Coupon {
  int? id;
  String? title;
  String? code;
  String? startDate;
  String? expireDate;
  double? minPurchase;
  double? maxDiscount;
  double? minDistance;
  double? maxDistance;
  dynamic startTime; // Can be null
  dynamic endTime; // Can be null
  double? discount;
  String? discountType;
  String? couponType;
  int? limit;
  int? status;
  int? restaurantId;
  String? restaurants;
  String? customers;
  String? createdAt;
  String? updatedAt;
  String? data;
  int? totalUses;
  int? private;
  int? maximumUsage;
  String? discountOn;
  int? newUser;
  int? buy;
  int? free;
  bool? freeDelivery;

  Coupon(
      {this.id,
      this.title,
      this.code,
      this.startDate,
      this.expireDate,
      this.minPurchase,
      this.maxDiscount,
      this.minDistance,
      this.maxDistance,
      this.startTime,
      this.endTime,
      this.discount,
      this.discountType,
      this.couponType,
      this.limit,
      this.status,
      this.restaurantId,
      this.restaurants,
      this.customers,
      this.createdAt,
      this.updatedAt,
      this.data,
      this.totalUses,
      this.private,
      this.maximumUsage,
      this.discountOn,
      this.newUser,
      this.buy,
      this.free,
      this.freeDelivery});

  factory Coupon.fromJson(Map<String, dynamic> json) {
    return Coupon(
        id: json['id'],
        title: json['title'],
        code: json['code'],
        startDate: json['start_date'],
        expireDate: json['expire_date'],
        minPurchase: json['min_purchase'],
        maxDiscount: json['max_discount'],
        minDistance: json['min_distance'],
        maxDistance: json['max_distance'],
        startTime: json['start_time'],
        endTime: json['end_time'],
        discount: json['discount'],
        discountType: json['discount_type'],
        couponType: json['coupon_type'],
        limit: json['limit'],
        status: json['status'],
        restaurantId: json['restaurant_id'],
        restaurants: json['restaurants'],
        customers: json['customers'],
        createdAt: json['created_at'],
        updatedAt: json['updated_at'],
        data: json['data'],
        totalUses: json['total_uses'],
        private: json['private'],
        maximumUsage: json['maximum_usage'],
        discountOn: json['discount_on'],
        newUser: json['new_user'],
        buy: json['buy'],
        free: json['free'],
        freeDelivery: json['free_delivery']);
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'code': code,
      'start_date': startDate,
      'expire_date': expireDate,
      'min_purchase': minPurchase,
      'max_discount': maxDiscount,
      'min_distance': minDistance,
      'max_distance': maxDistance,
      'start_time': startTime,
      'end_time': endTime,
      'discount': discount,
      'discount_type': discountType,
      'coupon_type': couponType,
      'limit': limit,
      'status': status,
      'restaurant_id': restaurantId,
      'restaurants': restaurants,
      'customers': customers,
      'created_at': createdAt,
      'updated_at': updatedAt,
      'data': data,
      'total_uses': totalUses,
      'private': private,
      'maximum_usage': maximumUsage,
      'discount_on': discountOn,
      'new_user': newUser,
      'buy': buy,
      'free': free,
      'free_delivery': freeDelivery
    };
  }
}
